
//本js获取登录信息
$(function(){
    //加载用户信息
    getLoginInfo();
});

	/**
	注 : 该js要在index.html页面引入

	公有变量缓存存储
*/
var LocalCache = {
	
	//项目名称
	projectName : "后台管理系统",
	
	//首页地址
	homePageUrl : "/views/main/index.html",
			
	//获取登录信息action地址
	loginInfoActionUrl : "/login/getLoginInfo",
	
	//获取菜单的action地址
	menuActionUrl : "/menu/getUserMenu",

	//登出action地址
	logoutActionUrl : "/login/logout",

	//皮肤("颜色由左边向右边渐变,两种颜色用 - 隔开,不设置默认为蓝色")
	skin : "black-gray",

	loginPageUrl : "/views/login/login.html"

}

//获取登陆信息
function getLoginInfo(){
	$.ajax({
		url : LocalCache.loginInfoActionUrl,
		type :'POST',
		dataType : "json",
		contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
		async : false , 
		success:function(rst){
			if(rst.code == 200){
				var data  = rst.data;
				checkMenuNo(data);
				var userName = data.userName;
				LocalCache.userInfo = data;
				$("#userName").attr("userId" , data.id + "").html(userName);
			}else if(rst.code == 401){
				window.top.location.href = LocalCache.loginPageUrl;
			}
		}
	});
}

//校验
function checkMenuNo(userInfo){
	
	if(userInfo.id == -1) {
		$(".permissionMenu").show();
		return;
	}
	
	menuNos = userInfo.menuNos;
	
	if(!menuNos || menuNos.length == 0){
		location.href = "/views/login/nopermission.html";
		return;
	}
	
	var menuNo = $("body").attr("menuNo");
	
	if(!menuNo ||  menuNo == "") return;
	
	if(menuNos.indexOf(menuNo) == -1){
		location.href = "/views/login/nopermission.html";
		return;
	}
	
	//按钮
	for(var i = 0 ; i < menuNos.length ; i ++){
		$(".permissionMenu." + menuNos[i]).show();
	}
	
}

/*未登录过滤器,发现未登录直接跳转登录页面*/
$(document).ajaxComplete(function(event, request, settings) {
	
	try {
		
		var rst = JSON.parse(request.responseText);
		
		if(rst.code == 401)
			window.top.location.href="/views/login/login.html";
		
		//ajax完成之后 , 校验下权限
		checkMenuNo(LocalCache.userInfo);
		
	} catch (e) {
		console.info(e);
	}
	
});


/*发送ajax前 , 在请求头加token*/
$(document).ajaxSend(function(event, request, settings) {
	
	var token = window.localStorage.getItem("token");
	
	if(!token || token == "")
		return;
	
	request.setRequestHeader("token" , token);
	
});




